package com.wswy.wzcx.ui.message;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.MessageDetail;
import com.wswy.wzcx.model.MessageMode;
import com.wswy.wzcx.module.MessageCenter;
import com.wswy.wzcx.module.base.CBaseFragment;
import com.wswy.wzcx.ui.activity.MineActivity;
import com.wswy.wzcx.ui.other.SimpleHttp;
import com.wswy.wzcx.ui.user.LoginHolderActivity;
import com.wswy.wzcx.widget.BadgeTextView;


/**
 * Created by wangduheng26 on 2018/1/19.
 * wangduheng26@gmail.com
 */

public class MessageFragment extends CBaseFragment implements View.OnClickListener {
    public static final String TAG = "MessageFragment";
    public static final int RQ_LOGIN = 0X1001;

    TextView tvSystemContent;
    TextView tvUserContent;

    TextView tvSocialContent;

    View vServer;
    View vActivity;

    View vSocial;

    BadgeTextView badgeSystem;
    BadgeTextView badgeUser;
    BadgeTextView badgeSocial;


    public static MessageFragment newInstance() {
        Bundle args = new Bundle();
        MessageFragment fragment = new MessageFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvSystemContent = view.findViewById(R.id.tv_message_content);
        tvUserContent = view.findViewById(R.id.tv_user_content);
        tvSocialContent = view.findViewById(R.id.tv_social_content);
        vServer = view.findViewById(R.id.ll_system);
        vActivity = view.findViewById(R.id.ll_user);
        vSocial = view.findViewById(R.id.ll_social);
        badgeSystem = view.findViewById(R.id.system_badge);
        badgeUser = view.findViewById(R.id.user_badge);
        badgeSocial = view.findViewById(R.id.social_badge);


        vServer.setOnClickListener(this);
        vActivity.setOnClickListener(this);
        vSocial.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        if (DataCenter.get().hasLogin()) {
            setupPage();
        } else {
            startActivityForResult(new Intent(getActivity(), LoginHolderActivity.class), RQ_LOGIN);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (DataCenter.get().hasLogin()) {
            setupPage();
        } else {
            finish();
        }


    }


    private void observeMsg() {


        MessageCenter.INSTANCE.getNewestLiveData().observeForever(
                new Observer<MessageMode>() {
                    @Override
                    public void onChanged(@Nullable MessageMode messageMode) {

                        if (messageMode != null) {

                            checkResult(messageMode);
                        }

                    }
                });


    }

    private void setupPage() {

        MessageCenter.INSTANCE.loadMsgUnreadCount(true);

    }

    private void checkResult(MessageMode messageMode) {
        if (messageMode != null && getContext() != null) {
            MessageDetail activityMessage = messageMode.systemMessage;
            MessageDetail serverMessage = messageMode.userMessage;

            MessageDetail socialMessage = messageMode.socialMessage;

            String content = null;
            int unread = 0;

            if (serverMessage != null) {
                content = serverMessage.content;
                unread = serverMessage.unreadTotal;
            } else {
                content = getString(R.string.empty_sys_msg);
            }

            tvSystemContent.setText(content);
            badgeSystem.setBadgeCount(unread);


            if (activityMessage != null) {
                content = activityMessage.content;
                unread = activityMessage.unreadTotal;
            } else {
                content = getString(R.string.empty_user_msg);
                unread = 0;
            }
            tvUserContent.setText(content);
            badgeUser.setBadgeCount(unread);

            if (socialMessage != null) {
                content = socialMessage.title;
                unread = socialMessage.unreadTotal;
            } else {
                content = getString(R.string.empty_social_msg);
                unread = 0;
            }


            if ("##点赞##".equals(content)) {
                tvSocialContent.setText(null);
                if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
                    tvSocialContent.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_like_blue, 0, 0, 0);
                }
            } else {
                tvSocialContent.setText(content);
            }


            badgeSocial.setBadgeCount(unread);

        }
    }

    @Override
    public String getFragmentTitle() {
        return "消息";
    }

    @Override
    public void onClick(View v) {
        if (v == vServer) {
            startActivity(MineActivity.asIntent(v.getContext(), MessageDetailFragment.TAG, MessageMode.MESSAGE_USER));
//            ToastUtils.showText("该功能暂未开放，敬请期待！");
            if (badgeSystem.getBadgeCount() > 0) {
                badgeSystem.setBadgeCount(0);
                //RxBus.getDefault().postWithCode(InternalMsgCode.CODE_MESSAGE_COUNT_CHANGE,"default");
                SimpleHttp.INSTANCE.setReader(MessageMode.MESSAGE_USER);
            }

        } else if (v == vActivity) {
            startActivity(MineActivity.asIntent(v.getContext(), MessageDetailFragment.TAG, MessageMode.MESSAGE_SYSTEM));
            if (badgeUser.getBadgeCount() > 0) {
                badgeUser.setBadgeCount(0);
                //RxBus.getDefault().postWithCode(InternalMsgCode.CODE_MESSAGE_COUNT_CHANGE,"default");
                SimpleHttp.INSTANCE.setReader(MessageMode.MESSAGE_SYSTEM);
            }

        } else if (v == vSocial) {
            startActivity(MineActivity.asIntent(v.getContext(), MessageDetailFragment.TAG, MessageMode.MESSAGE_SOCIAL));

            if (badgeSocial.getBadgeCount() > 0) {
                badgeSocial.setBadgeCount(0);
                //RxBus.getDefault().postWithCode(InternalMsgCode.CODE_MESSAGE_COUNT_CHANGE,"default");
                SimpleHttp.INSTANCE.setReader(MessageMode.MESSAGE_SOCIAL);
            }

        }
    }


    @Override
    public void onResume() {
        super.onResume();
        observeMsg();
    }

    @Override
    public void onPause() {
        super.onPause();
        MessageCenter.INSTANCE.getNewestLiveData().removeObservers(this);
    }

}
